package com.ohos.connent.accountcloud.biz.modular.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ohos.connent.accountcloud.biz.modular.dp.entity.DpEnterpriseInfo;
import com.ohos.connent.accountcloud.biz.modular.dp.entity.DpOrg;
import com.ohos.connent.accountcloud.biz.modular.dp.enums.DpOrgStatusEnum;
import com.ohos.connent.accountcloud.biz.modular.dp.param.DpEnterprisePageParam;
import com.ohos.connent.accountcloud.biz.modular.dp.service.DpEnterpriseService;
import com.ohos.connent.accountcloud.biz.modular.dp.service.DpOrgService;
import com.ohos.connent.accountcloud.biz.modular.org.param.OrgCertifiedParam;
import com.ohos.connent.accountcloud.biz.modular.org.param.OrgFrozenParam;
import com.ohos.connent.accountcloud.biz.modular.org.param.OrgIdParam;
import com.ohos.connent.accountcloud.biz.modular.org.param.OrgPageParam;
import com.ohos.connent.accountcloud.biz.modular.org.result.EnterpriseDetailResult;
import com.ohos.connent.accountcloud.biz.modular.org.service.OrgService;
import com.ohos.connent.accountcloud.common.exception.CommonException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * 运维平台企业管理Service实现类
 * @Author: ChenJiaHao
 * @Date: 2024/6/12 16:36
 * @Description:
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Resource
    private DpOrgService dpOrgService;
    @Resource
    private DpEnterpriseService dpEnterpriseService;

    @Override
    public Page<DpEnterpriseInfo> page(OrgPageParam orgPageParam) {
        return dpEnterpriseService.page(BeanUtil.toBean(orgPageParam, DpEnterprisePageParam.class));
    }

    @Override
    public EnterpriseDetailResult detail(OrgIdParam orgIdParam) {
        DpEnterpriseInfo dpEnterpriseInfo = dpEnterpriseService.queryEntity(orgIdParam.getId());
        return BeanUtil.toBean(dpEnterpriseInfo, EnterpriseDetailResult.class);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void certified(OrgCertifiedParam orgCertifiedParam) {
        DpOrgStatusEnum.validateCertified(orgCertifiedParam.getStatus());
        List<DpOrg> dpOrgList = dpOrgService.getOrgListByCode(orgCertifiedParam.getIds());
        if (CollectionUtils.isEmpty(dpOrgList)) {
            throw new CommonException("暂没有需要审核的企业");
        }

        dpOrgList.forEach(dpOrg -> dpOrg.setStatus(orgCertifiedParam.getStatus()));
        dpOrgService.updateBatchById(dpOrgList, dpOrgList.size());

        List<DpEnterpriseInfo> dpEnterpriseInfos = dpEnterpriseService.listByIds(orgCertifiedParam.getIds());
        dpEnterpriseInfos.forEach(dpEnterpriseInfo -> {
            Integer status = orgCertifiedParam.getStatus();
            dpEnterpriseInfo.setStatus(status);
            dpEnterpriseInfo.setCertifiedTime(DateTime.now());
            if (DpOrgStatusEnum.REJECTED.getStatus() == status) {
                if (BeanUtil.isNotEmpty(orgCertifiedParam.getReason())) {
                    dpEnterpriseInfo.setReason(orgCertifiedParam.getReason());
                }
                if (BeanUtil.isNotEmpty(orgCertifiedParam.getRemark())) {
                    dpEnterpriseInfo.setRemark(orgCertifiedParam.getRemark());
                }
            }
        });
        dpEnterpriseService.updateBatchById(dpEnterpriseInfos, dpEnterpriseInfos.size());
    }

    @Override
    public void frozen(OrgFrozenParam orgFrozenParam) {
        DpOrgStatusEnum.validateFrozen(orgFrozenParam.getStatus());
        List<DpOrg> dpOrgList = dpOrgService.list(Wrappers.lambdaQuery(DpOrg.class).in(DpOrg::getCode, orgFrozenParam.getIds()));
        if (CollectionUtils.isEmpty(dpOrgList)) {
            throw new CommonException("暂没有需要审核的企业");
        }
        dpOrgList.forEach(dpEnterpriseInfo -> dpEnterpriseInfo.setStatus(orgFrozenParam.getStatus()));
        dpOrgService.updateBatchById(dpOrgList, dpOrgList.size());

        List<DpEnterpriseInfo> dpEnterpriseInfos = dpEnterpriseService.listByIds(orgFrozenParam.getIds());
        dpEnterpriseInfos.forEach(dpEnterpriseInfo -> dpEnterpriseInfo.setStatus(orgFrozenParam.getStatus()));
        dpEnterpriseService.updateBatchById(dpEnterpriseInfos, dpEnterpriseInfos.size());
    }
}
